const app =getApp();
let WxParse = require('../../../wxParse/wxParse.js');
Page({
  data: {
    id:0,
    user:'',
    choosePeople:1, //前面是否有人排队 1-无 2-有
    chooseBox:2, //盲盒是否已售  1-已售 2-未售 
    isShow:false,
    isRuleShow:false,
    collectShow:false,  
    buyAllShow:false,
    datailData:[],
    boxsList:[],
    listHeight:'',
    collectList:[
      {
        id:1,
        src:'../../icon/icona.png',
        title:'柠檬绵绵'
      },
      {
        id:2,
        src:'../../icon/iconb.png',
        title:'雪糕绵绵'
      },
      {
        id:3,
        src:'../../icon/iconcc.png',
        title:'椰果绵绵'
      },
      {
        id:4,
        src:'../../icon/icond.png',
        title:'果酱绵绵'
      },
      {
        id:5,
        src:'../../icon/iconaa.png',
        title:'糖果绵绵'
      },
      {
        id:6,
        src:'../../icon/iconbb.png',
        title:'草莓绵绵'
      },
      {
        id:7,
        src:'../../icon/iconcc.png',
        title:'苹果绵绵'
      },
      {
        id:8,
        src:'../../icon/icondd.png',
        title:'西瓜绵绵'
      },
      {
        id:9,
        src:'../../icon/iconaa.png',
        title:'西瓜绵绵'
      },
      {
        id:10,
        src:'../../icon/iconbb.png',
        title:'西瓜绵绵'
      },
      {
        id:11,
        src:'../../icon/iconcc.png',
        title:'西瓜绵绵'
      }
    ],
    rule:''
  },
  //监听页面加载
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.data.id = options.id
    this.getseriesData(this.data.id )

    //获取终端高度
    var query = wx.createSelectorQuery();
    query.select('.list').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec((res) => {
      var listHeight = res[1].scrollHeight; // 获取list高度
      this.setData({
        listHeight:listHeight
      })
    })
  },
  //监听页面显示
  onShow: function () {},
  //获取盲盒信息
  getseriesData(id){
    var that = this;
    let token = wx.getStorageSync('token')
    app.util.request({
      url: app.do('index/getSeries'),
      method: 'Post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        id: id,
        token: token,
      },
      success: function (res) {
        if (res.data.code == "0000") {
          that.setData({
            datailData: res.data.data.seriesDetail,
            boxsList:res.data.data.newData,
          })
          //解析富文本
          //商品详情
          var content = res.data.data.seriesDetail.box_details;
          var rule = res.data.data.seriesDetail.box_rule;
          
          WxParse.wxParse('content', 'html', content, that,5)
          WxParse.wxParse('rule', 'html', rule, that,5)
          //商品规则
        } 
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  //换一盒
  getseriesList(){
    if(this.data.datailData.stock == 0){
      wx.showToast({
        title: '盲盒已售出哦~',
        icon: 'none',
        duration: 2000
      })
    }else{
      this.getseriesData(this.data.id)
    }
  },
  // 单个选择买盲盒
  ChooseBox(e){
    var that = this;
    // console.log('choosePeople',that.data.choosePeople)
    if(that.data.datailData.stock == 0){
      wx.showToast({
        title: '盲盒已售出哦~',
        icon: 'none',
        duration: 2000
      })
    }else{
        if(that.data.choosePeople==2){
          wx.showToast({
            title: '请先排队哦~',
            icon: 'none',
            duration: 2000
          })
        }else if(that.data.choosePeople==1){
            if(e.currentTarget.dataset.choosebox==1){
              // 未售
              wx.navigateTo({
                url: '../chooseOne/chooseOne?id='+that.data.id
              })
            }else if(e.currentTarget.dataset.choosebox==2){
              // 已售
              wx.showToast({
                title: '盲盒已售出哦~',
                icon: 'none',
                duration: 2000
              })
            }else if(e.currentTarget.dataset.choosebox==undefined){
              wx.navigateTo({
                url: '../chooseOne/chooseOne?id='+that.data.id
              })
            }
        }
    }
  },
  //端盒购买盲盒
  ChooseAllBox(){
    if(this.data.datailData.stock>=9){
        wx.navigateTo({
          url: '../chooseAll/chooseAll?id='+this.data.id
        })
    }else if(this.data.datailData.stock ==0){
        wx.showToast({
          title: '盲盒已售出哦~',
          icon: 'none',
          duration: 2000
        })
    }else{
        this.showBuyAllCover();
    }

  },
  //选择收货地址
  lookAddress(){
    // 正确流程：
    // 1.获取 权限状态
      wx.getSetting({
        success: (result) => {
          // 2. 获取权限状态 
          // 主要发现一些 属性名很怪异的时候 都要使用 [] 形式来获取属性
            const scopeAddres=result.authSetting["scope.address"];
          // 判断用户的权限状态
            if(scopeAddres===true||scopeAddres===undefined){
              wx.chooseAddress({
                success: (result1) => {
                  //获取数据
                  this.setData({
                    user:{
                      name:result1.userName,
                      address:result1.cityName + result1.countyName +result1.detailInfo,
                      tel:result1.telNumber
                    }
                  })
                }
              });
            }
            else{
              // 3. 用户 以前拒绝过授予权限  先引导用户打开权限页面
              wx.openSetting({
                success: (result2) => {
                  // 4.可以调用  获取收获地址代码
                  wx.chooseAddress({
                    success: (result3) => {
                      // 获取数据
                      this.setData({
                        user:{
                          name:result3.userName,
                          cityName:result3.cityName
                        }
                      })
                    }
                  });
                }
              });
            }
          }
      });
  },
  //确认支付
  paymentBtn(){
    let token = wx.getStorageSync('token')
    var that = this;
    if(!that.data.user.name){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      });
      return;
    }
    wx.showModal({
      title: '',
      content: '确定要支付吗？',
      success (res) {
        if (res.confirm) {
          app.util.request({
            url: app.do('index/getPayment'),
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              token: token,
              typeId:that.data.id,
              address:that.data.user.address,
              mobile:that.data.user.tel,
              realname:that.data.user.name,
              totalPrice:that.data.datailData.totalPrice,
              totalSum:that.data.datailData.optional
            },
            success: function(res) {
              if(res.data.code == "0000"){
                  let jsdk = res.data.data.jsdk;
                  //调用微信支付
                  that.pay(jsdk);
              }
              else if(res.data.code == "2001"){
                wx.navigateTo({
                  url: '../auth/auth',
                })
              }
              else{
                var text = res.data.message;
                wx.showToast({
                  title: text,
                  icon: 'none',
                  duration: 2000,
                })
              }
            },
            fail: function(res) {
              var text = res.data.message;
              wx.showToast({
                title: text,
                icon: 'none',
                duration: 2000,
              })
            }
          })
        }
        else if (res.cancel) { }
      }
    })

  },
  pay(jsdk){
    wx.requestPayment({
      nonceStr: jsdk.nonceStr,
      package: jsdk.package,
      paySign: jsdk.paySign,
      signType: 'MD5',
      timeStamp: jsdk.timeStamp,
      success (res) { 
        // this.hideBuyAllCover()
        wx.switchTab({
          url: '../center/center',
        })
      },
      fail (res) { }
    })
  },


  // 一次购买当前页盲盒
  hideBuyAllCover(){
    this.setData({
      buyAllShow: false
    })
  },
  showBuyAllCover(){
    if(this.data.datailData.stock ==0){
        wx.showToast({
          title: '盲盒已售出哦~',
          icon: 'none',
          duration: 2000
        })
    }else{
      this.setData({
        buyAllShow:true
      })
    }
  },
  // 商品详情
  hideCover(){
    this.setData({
      isShow: false
    })
  },
  showCover(){
    this.setData({
      isShow:true
    })
  },
  // 规则
  hideRuleCover(){
    this.setData({
      isRuleShow: false
    })
  },
  showRuleCover(){
    this.setData({
      isRuleShow:true
    })
  },
  // 收集
  hideCollectCover(){
    this.setData({
      collectShow: false
    })
  },
  showCollectCover(){
    this.setData({
      collectShow:true
    })
  },
})